package com.gm.blog.controller;

import com.gm.blog.enums.ResultEnum;
import com.gm.blog.pojo.Link;
import com.gm.blog.service.LinkService;
import com.gm.blog.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 友情链接
 * @author 29944
 */
@RestController
@RequestMapping("/link")
public class LinkController {
    @Autowired
    private LinkService linkService;
    /**
     * 添加
     * @param link
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<Object> saveLink(@RequestBody Link link) {
        if(link==null||link.getLinkName()==null||link.getLinkUrl()==null){
            return new Result<>(ResultEnum.ERROR);
        }else{
            linkService.save(link);
            return new Result<>(ResultEnum.SUCCESS);
        }
    }
    /**
     * 修改友情链接
     * @param link
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result<Object> updateLink(@RequestBody Link link){
        if(link==null||link.getLinkName()==null||link.getLinkUrl()==null){
            return new Result<>(ResultEnum.ERROR);
        }else{
            linkService.update(link);
            return new Result<>(ResultEnum.SUCCESS);
        }
    }
    /**
     * 更具ID获得单个友情链接
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById/{id}",method=RequestMethod.GET)
    public Result<Object> getLinkById(@PathVariable Integer id){
        Link linkById = linkService.getLinkById(id);
        return new Result<>(linkById);
    }
    /**
     * 从后台查询全部的友情链接
     * @return
     */
    @RequestMapping(value = "/getLinkAll",method = RequestMethod.GET)
    public Result<Object> getLinkAll(){
        List<Link> linkAll = linkService.getLinkAll();
        return new Result<>(linkAll);
    }
    /**
     * 更具ID来删除分类
     */
    @RequestMapping(value="/delById/{id}",method = RequestMethod.DELETE)
    public Result<Object> delById(@PathVariable Integer id){
        linkService.delete(id);
        return new Result<>(ResultEnum.SUCCESS);
    }
}
